<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-public-scheduling-doctor" :style="getStyle('viewArea')">
      <div class="scheduling-wrap flex-box">
        <div class="flex-box-left" :style="getStyle('sign')">
          {{ optionsInfo.header.content || '\u00a0' }}
        </div>
        <div class="flex-box-right" :class="viewDirectionName">
          <template v-for="(item,index) in doctorSchedulingInfo(doctorOriginalInfo?.length)">
            <div class="group-first group-first-column" v-if="optionsInfo.rowDirection" :key="`${widgetKey}_${index}`">
              <template v-for="(itemInfo,key) in item">
                <div class="default-info doctor-info" :key="key" :style="getStyle('doctorInfo')">
                  <div class="left-area" v-if="!doctorIsEmpty(itemInfo)">
                    <div class="picture-info">
                      <img :src="getDoctorPicture(itemInfo)" alt="医生" @error='handleError'/>
                    </div>
                    <div class="campus_area">
                      <ScrollX>{{ itemInfo.workerDegree || '\u00a0' }}</ScrollX>
                    </div>
                    <div class="dept-info">
                      <ScrollX>{{ itemInfo.deptName || '\u00a0' }}</ScrollX>
                    </div>
                  </div>
                  <div class="right-area" v-if="!doctorIsEmpty(itemInfo)">
                    <div class="doctor-name">
                      <div class="name" :style="getStyle('doctorName')">{{ itemInfo?.userName || '\u00a0' }}</div>
                      <div class="level" v-if="itemInfo?.workerTitle" :key="widgetKey">
                        <ScrollX>{{ itemInfo?.workerTitle || '\u00a0' }}</ScrollX>
                      </div>
                    </div>
                    <div class="doctor-describe">
                      <div class="container" :key="widgetKey">
                        <div class="header" v-if="false">[介绍]</div>
                        <div class="content" :style="getStyle('doctorDescribe')">
                          <template v-if="optionsInfo['schedulingContentFormat']===1">
                            <ScrollY :content="itemInfo?.workerIntro || '\u00a0'" :pageDuration="10"/>
                          </template>
                          <template v-if="optionsInfo['schedulingContentFormat']===2">
                            <div class="content-inside-slice">
                              {{ itemInfo?.workerIntro || '\u00a0' }}
                            </div>
                          </template>
                          <template v-if="optionsInfo['schedulingContentFormat']===3">
                            <div class="content-inside-omit">
                              {{ itemInfo?.workerIntro || '\u00a0' }}
                            </div>
                          </template>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </template>
            </div>
            <div class="group-first group-first-horizontal" v-if="!optionsInfo.rowDirection"
                 :key="`${widgetKey}_${index}`">
              <template v-for="(itemInfo,key) in item">
                <div class="default-info doctor-info" :key="key" :style="getStyle('doctorInfo')">
                  <div class="left-area" v-if="!doctorIsEmpty(itemInfo)">
                    <div class="picture-info">
                      <img :src="getDoctorPicture(itemInfo)" alt="医生" v-on:error="handleError">
                    </div>
                    <div class="campus_area">
                      <ScrollX>{{ itemInfo.workerDegree || '\u00a0' }}</ScrollX>
                    </div>
                    <div class="dept-info">
                      <ScrollX>{{ itemInfo.deptName || '\u00a0' }}</ScrollX>
                    </div>
                  </div>
                  <div class="right-area" v-if="!doctorIsEmpty(itemInfo)">
                    <div class="doctor-name">
                      <div class="name" :style="getStyle('doctorName')">{{ itemInfo?.userName || '\u00a0' }}</div>
                      <div class="level" v-if="itemInfo?.workerTitle" :key="widgetKey">
                        <ScrollX>{{ itemInfo?.workerTitle || '\u00a0' }}</ScrollX>
                      </div>
                    </div>
                    <div class="doctor-describe">
                      <div class="container" :key="widgetKey">
                        <div class="header" v-if="false">[介绍]</div>
                        <div class="content" :style="getStyle('doctorDescribe')">
                          <template v-if="optionsInfo['schedulingContentFormat']===1">
                            <ScrollY :content="itemInfo?.workerIntro || '\u00a0'" :pageDuration="10"/>
                          </template>
                          <template v-if="optionsInfo['schedulingContentFormat']===2">
                            <div class="content-inside-slice">
                              {{ itemInfo?.workerIntro || '\u00a0' }}
                            </div>
                          </template>
                          <template v-if="optionsInfo['schedulingContentFormat']===3">
                            <div class="content-inside-omit">
                              {{ itemInfo?.workerIntro || '\u00a0' }}
                            </div>
                          </template>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </template>
            </div>
          </template>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from "uuid";
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import SvgIcon from "@c/public/svg-icon/index.vue";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import ScrollY from "@c/public/seamlessScroll/ScrollY.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

export default {
  name: "scene-public-scheduling-doctor2-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollY,
    SvgIcon,
    ScrollX,
    CompOperate,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */
      type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */
      type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  data() {
    return {
      optionsInfo: Array(),
      widgetKey: uuid(),
      header: require("@a/images/design/header.jpeg"),
      header2: require("@/components/assets/images/design/header.jpeg"),
    }
  },

  computed: {
    //生成唯一的字符串
    getOnlyId() {
      return function () {
        return uuid()
      }
    },

    // 判断医生是否为空
    doctorIsEmpty(){
      return function (row) {
        return this.$lodash.isEmpty(row)
      }
    },

    // 模板的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let Obj = Object();
        let doctor = (info?.list);
        let header = (info?.header);
        switch (val) {
          case "viewArea":
            Reflect.set(Obj, "height", pxToVw(info.areaHeight));
            return Obj;
          case "sign":
            Reflect.set(Obj, "color", (header?.color));
            Reflect.set(Obj, "fontWeight", (header?.fontWeight));
            Reflect.set(Obj, "background", (header?.background));
            Reflect.set(Obj, "fontSize", pxToVw(header?.fontSize));
            return Obj;
          case "doctorInfo":
            Reflect.set(Obj, "background", (doctor?.background));
            return Obj;
          case "doctorName":
            Reflect.set(Obj, "color", (doctor.doctorNameFontColor));
            Reflect.set(Obj, "fontSize", pxToVw(doctor.doctorNameFontSize));
            Reflect.set(Obj, "fontWeight", (doctor.doctorNameFontWeight));
            return Obj;
          case "doctorDescribe":
            Reflect.set(Obj, "color", (doctor.doctorDescribeFontColor));
            Reflect.set(Obj, "fontSize", pxToVw(doctor.doctorDescribeFontSize));
            Reflect.set(Obj, "fontWeight", (doctor.doctorDescribeFontWeight));
            return Obj;
        }
      };
    },

    // 视图排列方式样式类名称
    viewDirectionName() {
      let number = 1;
      let {patientListRow, patientListCol, rowDirection, trendsView = true} = this.optionsInfo;
      let originalInfo = this.doctorOriginalInfo;
      let len = originalInfo?.length || 1;
      let total = (patientListRow * patientListCol);
      if (!trendsView) {
        number = 4
      } else {
        number = len > total ? 4 : len
      }
      return rowDirection ?
          `flex-box-right-column flex-box-right-column-${number}` :
          `flex-box-right-horizontal flex-box-right-horizontal-${number}`;
    },

    // 获取原始数据
    doctorOriginalInfo() {
      let flattenArr;
      let devType = (this.devType);
      let {patientListRow, patientListCol, trendsView = true} = this.optionsInfo;
      let total = (patientListRow * patientListCol);
      let doctorListInfo = this.$store.getters['getSchedulingDoctorInfo2'];
      let listInfo = devType ? doctorListInfo['apiData'] || [] : doctorListInfo['mockData'] || [];
      let filterArr = this.$lodash.filter(listInfo, {'type': 2}) || []
      if (trendsView) {
        flattenArr = [...filterArr]
      } else {
        if (filterArr?.length >= total) {
          flattenArr = filterArr.slice(0, total);
        } else {
          let fillArr = (new Array(total - filterArr.length).fill({}));
          flattenArr = [...filterArr, ...fillArr];
        }
      }
      return flattenArr
    },

    //获切割数据
    doctorSchedulingInfo() {
      return function (chunkNumber) {
        let flattenArr;
        let {patientListRow, patientListCol, trendsView = true} = this.optionsInfo;
        let total = (patientListRow * patientListCol);
        let filterArr = this.doctorOriginalInfo;
        if (trendsView) {
          console.log('动态')
          console.log(chunkNumber)
          switch (chunkNumber) {
            case 0:
              let fillArr = (new Array(1).fill({}));
              return this.$lodash.chunk(fillArr, 1)
            case 1:
            case 2:
              flattenArr = filterArr.slice(0, chunkNumber);
              return this.$lodash.chunk(flattenArr, chunkNumber) || []
            case 3:
              flattenArr = filterArr.slice(0, chunkNumber);
              return this.$lodash.chunk(flattenArr, 2) || []
            case 4:
            default:
              if (filterArr?.length >= total) {
                flattenArr = filterArr.slice(0, total);
              } else {
                let fillArr = (new Array(total - filterArr.length).fill({}));
                flattenArr = [...filterArr, ...fillArr];
              }
              return this.$lodash.chunk(flattenArr, 2) || []
          }
        } else {
          console.log(2)
          if (filterArr?.length >= total) {
            flattenArr = filterArr.slice(0, total);
          } else {
            let fillArr = (new Array(total - filterArr.length).fill({}));
            flattenArr = [...filterArr, ...fillArr];
          }
          return this.$lodash.chunk(flattenArr, 2) || []
        }

      }
    },

    // 获取医生头像
    getDoctorPicture() {
      return function (row) {
        let header = '/tpleditor/resource/assets/images/header.png';
        return (row?.workerPicture) || header
      }
    },
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件数据
    async executeMethod(key, data) {
      switch (key) {
        case "publishListInfo":
          this.widgetKey = uuid();
          let publicListInfo = this.$lodash.cloneDeep(data);
          await this.$store.dispatch('setSchedulingDoctorInfo2', publicListInfo)
          break
      }
    },

    handleError(event) {
      event.target.src = '/tpleditor/resource/assets/images/header.png'
    }

  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        let key = (val.options.config);
        this.widgetKey = uuid();
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-public-scheduling-doctor {
  color: #333;
  height: 100%;
  line-height: 100%;
  display: flex;
  justify-content: center;
  flex-direction: column;
  box-sizing: border-box;
  background: transparent;

  .scheduling-wrap {
    height: calc(100%);
    display: flex;
    flex-direction: row;
    overflow: hidden;
    justify-content: space-between;

    //标题
    .flex-box-left {
      flex-basis: 170px;
      min-width: calc(170px);
      background: #0059B3;
      writing-mode: vertical-rl; /* 文字从上到下，从右到左排列 */
      font-weight: 500;
      font-size: 70px;
      height: 100%;
      color: #FFFFFF;
      letter-spacing: 30px;
      align-self: center;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;
    }

    //医生信息-通用
    .flex-box-right {
      height: calc(100%);
      box-sizing: border-box;
      flex-basis: calc(100% - 170px);
      min-width: calc(100% - 170px);
      overflow: hidden;

      .group-first {
        width: calc(100%);
        box-sizing: border-box;

        .default-info {
          flex: 1;
          flex-shrink: 0;
          background-color: rgba(229, 234, 239, 1);
          display: flex;
          overflow: hidden;
          box-sizing: border-box;
          border-radius: 16px;
          padding: 24px 12px 16px 12px;
        }

        //医生
        .doctor-info {

          .left-area {
            width: 236px;
            height: 100%;
            overflow: hidden;
            flex-shrink: 0;

            .picture-info {
              overflow: hidden;
              width: 100%;
              height: auto;
              border-radius: 12px;

              img {
                width: 100%;
                height: 100%;
                object-fit: cover;
              }
            }

            .campus_area {
              color: #ed4014;
              font-size: 32px;
              background: transparent;
              line-height: normal;
              text-align: center;
              padding: 0 10px;
              font-weight: bold;
              margin-top: 16px;
            }

            .dept-info {
              font-size: 48px;
              background: transparent;
              line-height: normal;
              text-align: center;
              padding: 0 10px;
              font-weight: bold;
              @include singleEllipsis(100%);
            }
          }

          .right-area {
            display: flex;
            flex-direction: column;
            margin-left: 12px;
            overflow: hidden;
            flex-grow: 1;
            padding: 0 8px;
            box-sizing: border-box;

            .doctor-name {
              width: 100%;
              display: flex;
              height: 108px;
              flex-direction: row;
              align-items: center;
              overflow: hidden;
              box-sizing: border-box;

              .name {
                line-height: initial;
                white-space: nowrap;
                flex-shrink: 0;
                color: black;
                font-size: 56px;
                font-weight: bold;
                box-sizing: border-box;
                @include singleEllipsis(280px);
              }

              .level {
                font-size: 38px;
                color: white;
                display: flex;
                align-self: end;
                align-items: center;
                justify-content: center;
                max-width: calc(100% - 280px);
                line-height: initial;
                box-sizing: border-box;
                white-space: nowrap;
                background: #0059B3;
                overflow: hidden;
                border-radius: calc(12px / 2);
                padding: calc(4px / 2) calc(24px / 2);
                flex-basis: 0;
                flex-grow: 1;
              }
            }

            .doctor-describe {
              height: calc(100% - 108px - 24px);
              overflow: hidden;
              margin-top: 18px;
              box-sizing: border-box;

              .container {
                width: 100%;
                height: calc(100%);
                line-height: normal;
                box-sizing: border-box;
                font-size: 34px;
                color: rgba(51, 51, 51, 0.8);
                text-align: justify;
                display: flex;
                flex-direction: column;

                .header {
                  min-width: 64px;
                  font-weight: 400;
                  color: #0059B3;
                  padding-bottom: 10px;
                  box-sizing: border-box;
                }

                .content {
                  height: calc(100% - 0px);
                  font-size: 30px;
                  color: #333333;
                  font-weight: 400;
                  line-height: 1.5;

                  .content-inside-omit {
                    width: 100%;
                    height: 100%;
                    position: relative;
                  }
                }
              }
            }
          }
        }

      }

    }


    //医生信息-水平
    .flex-box-right-column {
      display: flex;
      flex-direction: column;
      padding-left: 16px;
      box-sizing: border-box;

      .group-first-column {
        width: 100%;
        flex-grow: 1;
        flex-shrink: 0;
        flex-basis: 0;
        display: flex;
        overflow: hidden;
        flex-direction: row;
        align-content: flex-start;
        box-sizing: border-box;
        margin-top: 16px;

        &:first-child {
          margin-top: 0;
        }

        .doctor-info {
          min-width: calc(((100% - 16px) / 2)) !important;
          max-width: calc(((100% - 16px) / 2)) !important;
          margin-right: 16px;
          box-sizing: border-box;

          &:last-child {
            margin-right: 0 !important;
          }
        }
      }

      .default-info {
        min-width: calc(100% / 2);
        max-width: calc(100% / 2);
      }
    }

    //医生信息-水平-(1位医生)
    .flex-box-right-column-1 {
      .group-first-column .default-info {
        min-width: calc(100% / 1) !important;
        max-width: calc(100% / 1) !important;
      }
    }

    .flex-box-right-column-2 {
      .group-first-column {
        display: flex;
        flex-direction: column;

        .default-info {
          min-width: calc(100% / 1) !important;
          max-width: calc(100% / 1) !important;
          margin-bottom: 16px;

          &:last-child {
            margin-bottom: 0;
          }
        }
      }
    }


    //医生信息-垂直
    .flex-box-right-horizontal {
      display: flex;
      flex-direction: row;
      padding-left: 16px;
      box-sizing: border-box;

      .group-first-horizontal {
        width: calc(100%);
        flex-grow: 1;
        flex-shrink: 0;
        flex-basis: 0;
        display: flex;
        overflow: hidden;
        flex-direction: column;
        align-content: flex-start;
        box-sizing: border-box;
        margin-right: 16px;

        &:last-child {
          margin-right: 0;
        }

        .default-info {
          margin-top: 16px;
          min-width: calc(100%);
          max-width: calc(100%);

          &:first-child {
            margin-top: 0;
          }
        }
      }
    }

    .flex-box-right-horizontal-3 {
      .group-first-horizontal .default-info{
        max-height: calc(100% / 2);
        min-height: calc(100% / 2);
        height: calc(100% / 2);
      }
    }

  }

}
</style>